package com.feel.easycloud.service.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feel.easycloud.common.EasyCloudUtils;
import com.feel.easycloud.entity.SysUser;
import com.feel.easycloud.enums.ApiResultEnum;
import com.feel.easycloud.mapper.SysUserMapper;
import com.feel.easycloud.service.SystemUserService;
import com.feel.easycloud.utils.ApiResult;
import com.feel.easycloud.vo.CreateAndUpdateSystemUserVo;
import com.feel.easycloud.vo.login.LoginUserRequestVo;
import com.feel.easycloud.vo.singlebasevo.BasePageQueryVo;
import com.feel.easycloud.vo.singlebasevo.SingleEntityRequestVo;
import com.feel.easycloud.vo.user.UserRestPasswordRequestVo;
import com.feel.easycloud.vo.user.UserRestPasswordResponseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SystemUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SystemUserService {

    @Override
    public void createSystemUser(CreateAndUpdateSystemUserVo requestVo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(requestVo, sysUser);
        sysUser.setId(EasyCloudUtils.UUID());
        baseMapper.insert(sysUser);
    }

    @Override
    public void updateSystemUser(CreateAndUpdateSystemUserVo requestVo) {
        SysUser sysUser = baseMapper.selectById(requestVo.getId());
        SysUser updateUser = new SysUser();
        BeanUtils.copyProperties(requestVo,updateUser);
        updateUser.setId(sysUser.getId());
        baseMapper.updateById(updateUser);
    }

    @Override
    public void deleteSystemUser(SingleEntityRequestVo requestVo) {
        baseMapper.deleteById(requestVo.getId());
    }

    @Override
    public CreateAndUpdateSystemUserVo getSystemUser(SingleEntityRequestVo requestVo) {
        SysUser sysUser = baseMapper.selectById(requestVo.getId());
        CreateAndUpdateSystemUserVo response = new CreateAndUpdateSystemUserVo();
        BeanUtils.copyProperties(sysUser,response);
        return response;
    }

    @Override
    public PageInfo getSystemUserPageList(BasePageQueryVo requestVo) {

        PageHelper.startPage(TypeUtils.castToInt(requestVo.getPage())
                ,TypeUtils.castToInt(requestVo.getSize()));
        List<SysUser> sysUsers = baseMapper.selectList(null);
        PageInfo pageInfo = new PageInfo(sysUsers);
        List<SysUser> list = pageInfo.getList();
        List<CreateAndUpdateSystemUserVo> collect = list.stream().map(x -> {
            CreateAndUpdateSystemUserVo vo = new CreateAndUpdateSystemUserVo();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(collect);
        return pageInfo;
    }

    @Override
    public ApiResult<SysUser> login(String loginType, LoginUserRequestVo requestVo) {
        QueryWrapper<SysUser> loginParam = new QueryWrapper();
        loginParam.eq("phone",requestVo.getPhone());
        List<SysUser> sysUser = baseMapper.selectList(loginParam);

        if(null == sysUser){
            return ApiResult.fail(ApiResultEnum.CODE_100004);
        }

        SysUser sysUser1 = sysUser.get(0);

        JWT.create().withAudience(sysUser1.getId())
                .sign(Algorithm.HMAC256(sysUser1.getPassword()));
        return ApiResult.success(sysUser);
    }

    @Override
    public ApiResult<SysUser> checkEmail(String email) {

        QueryWrapper<SysUser> loginParam = new QueryWrapper();
        loginParam.eq("email",email);
        List<SysUser> sysUser = baseMapper.selectList(loginParam);

        if(null == sysUser){
            return ApiResult.fail(ApiResultEnum.CODE_100004);
        }
        return ApiResult.success(sysUser);
    }

    @Override
    public ApiResult<UserRestPasswordResponseVo> restPassword(UserRestPasswordRequestVo requestVo) {

        QueryWrapper<SysUser> loginParam = new QueryWrapper();
        loginParam.eq("email",requestVo.);
        List<SysUser> sysUser = baseMapper.selectList(loginParam);


        return null;
    }
}
